using System;

namespace CLELCore.Assembly
{
	public class Token
	{
		private byte _table;
		private int _row;

		public byte Table
		{
			get
			{
				return _table;
			}
			set
			{
				_table = value;
			}
		}
		public int Row
		{
			get
			{
				return _row;
			}
			set
			{
				_row = value & 0xffffff;
			}
		}

		public Token(int encoded_token)
		{
			_table = Bits.ByteFromInt(3,encoded_token);
			_row = encoded_token & 0xffffff;
		}

		public Token(byte table,int row)
		{
			_table = table;
			_row = row & 0xffffff;
		}

		public Token(Token t)
		{
			if(t == null)
				throw new ArgumentException("Token cannot be null");
			_table = t.Table;
			_row = t.Row;
		}

		public Token Copy()
		{
			return new Token(_table,_row);
		}

		public int GetEncodedToken()
		{
			return (_table<<24) | _row;
		}

		public override int GetHashCode()
		{
			return GetEncodedToken();
		}

		public override bool Equals(object obj)
		{
			if(obj is Token)
			{
				Token t = (Token)obj;
				if((t.Table == _table) && (t.Row == _row))
					return true;
			}
			return false;
		}
		
		public override string ToString()
		{
			return "0x" + String.Format("{0:X}",GetEncodedToken());
		}
	}
}
